import sys
import numpy

def print_version():
    id_string = '$Id: read_molecule.py 132 2013-05-03 12:58:16Z pepijndevos@gmail.com $'
    revision_number = id_string.split()[2]
    revision_date = id_string.split()[3]
    print 'You are running version ',revision_number

def check_line( input_string, atom ):
    return input_string.startswith( 'ATOM' ) and input_string.split()[2] == atom

def calculate_means( text_lines, atom_name ):
    x_values = []
    y_values = []
    z_values = []
    for line in text_lines:
        if check_line( line, atom_name ):
            line_list = line.split()
            x_values.append( float(line_list[4]) )
            y_values.append( float(line_list[5]) )
            z_values.append( float(line_list[6]) )

    x_avg = numpy.mean( x_values )
    y_avg = numpy.mean( y_values )
    z_avg = numpy.mean( z_values )

    return x_avg,y_avg,z_avg

def main(filename):
    print_version()
    buffer = open( filename, 'r' )

    text_lines = buffer.readlines()

    x_avg,y_avg,z_avg = calculate_means( text_lines, 'C')

    print 'The average x value is :',x_avg
    print 'The average y value is :',y_avg
    print 'The average z value is :',z_avg

if __name__ =="__main__":
    main(sys.argv[1])
